<template>
  <a-modal
    :title="modalTitle"
    style="top: 20px"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form">
      <a-form-item style="display: none">
        <a-input v-decorator="['id']" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="岗位名称">
        <a-input
          placeholder="岗位名称"
          v-decorator="['postName', { rules: [{ required: true, message: '请输入岗位名称' }] }]"
        />
      </a-form-item>

      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="岗位编码">
        <a-input
          placeholder="岗位编码"
          v-decorator="['postCode', { rules: [{ required: true, message: '请输入岗位编码' }] }]"
        />
      </a-form-item>

      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="显示排序">
        <a-input-number v-decorator="['sort',{initialValue:'1',rules: [{ required: true, message: '请输入顺序数字' }]}]" :min="1" :max="100" :size="6" />
      </a-form-item>
      <a-form-item label="岗位状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
        <a-radio-group v-decorator="['status', { initialValue: '0' }]">
          <a-radio :value="'0'">正常</a-radio>
          <a-radio :value="'1'">停用</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="备注">
        <TextArea  placeholder="备注" v-decorator="['remark']" />
      </a-form-item>

      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="创建时间" v-if="mdl.createTime">
        <span>{{ mdl.createTime }}</span>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="更新时间" v-if="mdl.updateTime">
        <span>{{ mdl.updateTime }}</span>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { savePost } from '@/api/system/sysPost'
import TextArea from '@/components/TextArea'
import pick from 'lodash.pick'
export default {
  name: 'PostModal',
  components: {
    TextArea,
  },
  data() {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      mdl: {},
      form: this.$form.createForm(this),
      modalTitle: undefined
    }
  },
  beforeCreate() {},
  created() {},
  methods: {
    add() {
      this.modalTitle='添加岗位'
      this.form.resetFields()
      this.edit({ id: 0 })
    },
    edit(record) {
      if(record.id>0){
        this.modalTitle='编辑岗位'
      }
      this.mdl = Object.assign(record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.mdl, 'id', 'postName', 'postCode', 'status', 'remark', 'sort'))
      })
    },
    handleSubmit(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          this.confirmLoading = true
          savePost(values)
            .then((res) => {
              if (res.code === 200) {
                this.$message.success(res.message)
                this.$emit('ok')
                this.visible = false
              } else {
                this.$message.success(res.message)
              }
            })
            .catch(() => {
              this.$message.error('系统错误，请稍后再试')
            })
            .finally(() => {
              this.confirmLoading = false
            })
        }
      })
    },
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  },
}
</script>
